package com.online.service_edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.online.service_edu.entity.EduChapter;
import com.online.service_edu.entity.EduVideo;
import com.online.service_edu.entity.treeChapter.ChapterVO;
import com.online.service_edu.entity.treeChapter.VideoVO;
import com.online.service_edu.mapper.EduChapterMapper;
import com.online.service_edu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.online.service_edu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author hdc2008
 * @since 2021-02-11
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public List<ChapterVO> getChapterList(String id) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
            wrapper.eq("course_id",id);
            wrapper.orderByAsc("sort");
        List<EduChapter> eduChapters = baseMapper.selectList(wrapper);
        List<ChapterVO> chapterVOList=new ArrayList<>();
        for (EduChapter eduChapter : eduChapters) {
            ChapterVO chapterVO=new ChapterVO();
            BeanUtils.copyProperties(eduChapter,chapterVO);
            QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
            videoWrapper.eq("course_id",id);
            videoWrapper.eq("chapter_id",eduChapter.getId());
            videoWrapper.orderByAsc("sort");
            List<EduVideo> videoList = eduVideoService.list(videoWrapper);

            List<VideoVO> videoVOList=new ArrayList<>();
            for (EduVideo eduVideo : videoList) {
                VideoVO videoVO = new VideoVO();
                BeanUtils.copyProperties(eduVideo,videoVO);
                videoVOList.add(videoVO);
            }
            chapterVO.setEduVideoList(videoList);
            chapterVOList.add(chapterVO);
        }
        return chapterVOList;
    }

    @Override
    public boolean removeChapterById(String id) {
        List<EduVideo> eduVideoList=eduVideoService.getVideoListByChapterId(id);
        if(eduVideoList.size()==0){
            baseMapper.deleteById(id);
            return true;
        }
        return false;
    }
}
